<% @subtabs = worker_hosts.keys.sort unless worker_hosts.size == 1 %>

<% if params[:id] && worker = Resque::Worker.find(params[:id]) %>

  <h1>Worker <%= worker %></h1>
  <table class='workers'>
    <tr>
      <th>&nbsp;</th>
      <th>Host</th>
      <th>Pid</th>
      <th>Started</th>
      <th>Heartbeat</th>
      <th>Queues</th>
      <th>Processed</th>
      <th>Failed</th>
      <th>Processing</th>
    </tr>
    <tr>
      <td class='icon'><img src="<%=u state = worker.state %>.png" alt="<%= state %>" title="<%= state %>"></td>

      <% host, pid, queues = worker.to_s.split(':') %>
      <td><%= host %></td>
      <td><%= pid %></td>
      <td><span class="time"><%= worker.started %></span></td>
      <td><span class="time"><%= worker.heartbeat %></span></td>
      <td class='queues'><%= queues.split(',').map { |q| '<a class="queue-tag" href="' + u("/queues/#{q}") + '">' + q + '</a>'}.join('') %></td>
      <td><%= worker.processed %></td>
      <td><%= worker.failed %></td>
      <td class='process'>
        <% data = worker.processing || {} %>
        <% if data['queue'] %>
          <code><%= data['payload']['class'] %></code>
          <small><a class="queue time" href="<%=u "/working/#{worker}" %>"><%= data['run_at'] %></a></small>
        <% else %>
          <span class='waiting'>Waiting for a job...</span>
        <% end %>
      </td>
    </tr>
  </table>

<% elsif params[:id] && !worker_hosts.keys.include?(params[:id]) && params[:id] != 'all' %>

<h1>Worker doesn't exist</h1>

<% elsif worker_hosts.size == 1 || params[:id] %>

  <% if worker_hosts.size == 1 || params[:id] == 'all' %>
    <% workers = Resque.workers %>
  <% else %>
    <% workers = worker_hosts[params[:id]].map { |id| Resque::Worker.find(id) } %>
  <% end %>

  <h1 class='wi'><%= workers.size %> Workers</h1>
  <p class='intro'>The workers listed below are all registered as active on your system.</p>
  <table class='workers'>
    <tr>
      <th>&nbsp;</th>
      <th>Where</th>
      <th>Queues</th>
      <th>Processing</th>
    </tr>
    <% for worker in (workers = workers.sort_by { |w| w.to_s }) %>
    <tr class="<%=state = worker.state%>">
      <td class='icon'><img src="<%=u state %>.png" alt="<%= state %>" title="<%= state %>"></td>

      <% host, pid, queues = worker.to_s.split(':') %>
      <td class='where'><a href="<%=u "workers/#{worker}"%>"><%= host %>:<%= pid %></a></td>
      <td class='queues'><%= queues.split(',').map { |q| '<a class="queue-tag" href="' + u("/queues/#{q}") + '">' + q + '</a>'}.join('') %></td>

      <td class='process'>
        <% data = worker.processing || {} %>
        <% if data['queue'] %>
          <code><%= data['payload']['class'] %></code>
          <small><a class="queue time" href="<%=u "/working/#{worker}" %>"><%= data['run_at'] %></a></small>
        <% else %>
          <span class='waiting'>Waiting for a job...</span>
        <% end %>
      </td>
    </tr>
    <% end %>
    <% if workers.empty? %>
    <tr>
      <td colspan='4' class='no-data'>There are no registered workers</td>
    </tr>
    <% end %>
  </table>
  <%=poll%>

<% else %>
  <% @subtabs = [] %>
  <h1 class='wi'>Workers</h1>
  <p class='intro'>The hostnames below all have registered workers. Select a hostname to view its workers, or "all" to see all workers.</p>
  <table class='queues'>
    <tr>
      <th>Hostname</th>
      <th>Workers</th>
    </tr>
    <% for hostname, workers in worker_hosts.sort_by { |h,w| h } %>
    <tr>
      <td class='queue'><a class="queue" href="<%= u "workers/#{hostname}" %>"><%= hostname %></a></td>
      <td class='size'><%= workers.size %></td>
    </tr>
    <% end %>
    <tr class="failed">
      <td class='queue failed'><a class="queue" href="<%= u "workers/all" %>">all workers</a></td>
      <td class='size'><%= Resque.workers.size %></td>
    </tr>
  </table>


<% end %>
